﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace StockAna
{
    class StockPrize
    {
        public string code { get; set; }
        public double prize { get; set; }
        public string name { get; set; }

        public static async Task<int> UpdateAll(Action<int,int> progress)
        {
            SQLiteDataReader cntRd = Config.ReadSql("SELECT COUNT(*) FROM stock_prizes;");
            cntRd.Read();
            int cnt = cntRd.GetInt32(0);
            progress(0, cnt);

            int getCnt = 0;
            int zeroCnt = 0;
            SQLiteDataReader reader = Config.ReadSql("SELECT * FROM stock_prizes;");
            while (reader.Read())
            {
                string code = reader.GetString(0);
                string name = "NULL";
                double prize = GetPrize(code, out name);
                Config.RunSql(string.Format("UPDATE stock_prizes SET prize={0}, name='{2}' WHERE code='{1}';", prize, code, name));
                
                getCnt += 1;
                progress(getCnt, cnt);

                if (prize < 0.01)
                {
                    zeroCnt += 1;
                }
            }

            return zeroCnt;
        }

        public static void LoadFile(string path)
        {
            Config.RunSql("DELETE FROM stock_prizes;");

            StreamReader r = new StreamReader(path);
            string ln;
            while ((ln = r.ReadLine())!=null)
            {
                ln.Trim();
                Config.RunSql(string.Format("INSERT INTO stock_prizes VALUES('{0}',0,'NULL');", ln));
            }
        }

        public static StockPrize Find(string code)
        {
            SQLiteDataReader reader = Config.ReadSql(string.Format("SELECT * FROM stock_prizes WHERE code='{0}' LIMIT 1;", code));
            if (reader.Read())
            {
                StockPrize sp = new StockPrize();
                sp.code = code;
                sp.prize = reader.GetDouble(1);
                sp.name = reader.GetString(2);
                return sp;
            }

            return new StockPrize();
        }

        private static double GetPrize(string code, out string name)
        {
            name = "NULL";
            HttpWebRequest req = WebRequest.Create("http://hq.sinajs.cn/list=" + code) as HttpWebRequest;
            req.Method = "GET";
            HttpWebResponse rsp = req.GetResponse() as HttpWebResponse;
            StreamReader reader = new StreamReader(rsp.GetResponseStream(), Encoding.GetEncoding("GBK"));
            string data = reader.ReadToEnd();

            int left = data.IndexOf("\"") + 1;
            int right = data.IndexOf("\"", left);
            string arrdata = data.Substring(left, right - left);
            string[] arr = arrdata.Split(',');
            if (arr.Length < 4)
            {
                return 0;
            }

            name = arr[0];
            return Convert.ToDouble(arr[3]);
        }
    }
}
